package com.dykj.fireFighting.web.base.location;

import java.util.*;

import com.dykj.fireFighting.api.base.location.entity.LocationDevice;
import com.dykj.fireFighting.api.base.location.entity.dto.LocationDeviceDto;
import com.dykj.fireFighting.api.base.location.service.ILocationDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 定位设备接口
 *
 * @Author LIJIANJIE
 * @Date  2021-11-18 15:39:06
 */
@Slf4j
@Api(value = "定位设备接口", tags = {"定位设备接口"})
@RestController
@RequestMapping("/locationDevice")
public class LocationDeviceController {

    @DubboReference
    private ILocationDeviceService locationDeviceService;

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 定位设备列表
     *
     * @return
     */
    @AutoLog(value = "定位设备-分页列表查询")
    @ApiOperation(value = "定位设备分页列表", notes = "定位设备分页列表")
    @GetMapping("/list")
    @RequiresPermissions("/locationDevice/list")
    public CommonResult<PageVo<LocationDevice>> treeGrid(LocationDeviceDto commonDto) {
        PageVo<LocationDevice> page = locationDeviceService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 定位设备列表
     */
    @AutoLog(value = "定位设备-列表查询")
    @ApiOperation(value = "定位设备列表", notes = "定位设备列表")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public CommonResult<List<LocationDevice>> selectList() {
        List<LocationDevice> list = locationDeviceService.selectList();
        return CommonResult.success(list);
    }

    /**
     * 获取定位设备信息
     */
    @AutoLog(value = "定位设备-获取定位设备信息")
    @ApiOperation(value = "获取定位设备信息", notes = "获取定位设备信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("/locationDevice/info")
    public CommonResult<LocationDevice> info(@PathVariable("id") String id){
        LocationDevice locationDevice = locationDeviceService.getById(id);
        return CommonResult.success(locationDevice);
    }

    /**
     * 保存定位设备信息
     */
    @AutoLog(value = "定位设备-保存定位设备信息")
    @ApiOperation(value = "保存定位设备信息", notes = "保存定位设备信息")
    @PostMapping("/save")
    @RequiresPermissions("/locationDevice/save")
    public CommonResult save(@Valid @RequestBody LocationDevice locationDevice) {
        try {
            locationDevice.setId(UUIDUtils.get32UUID());
            locationDevice.setCreateTime(new Date());
            locationDevice.setCreateBy(getName());
            locationDeviceService.save(locationDevice);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改定位设备信息
     */
    @AutoLog(value = "定位设备-修改定位设备信息")
    @ApiOperation(value = "修改定位设备信息", notes = "修改定位设备信息")
    @PostMapping("/update")
    @RequiresPermissions("/locationDevice/update")
    public CommonResult update(@Valid @RequestBody LocationDevice locationDevice) {
        try {
            locationDevice.setUpdateTime(new Date());
            locationDevice.setUpdateBy(getName());
            locationDeviceService.updateById(locationDevice);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除定位设备信息
     */
    @AutoLog(value = "定位设备-删除定位设备信息")
    @ApiOperation(value = "删除定位设备信息", notes = "删除定位设备信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("/locationDevice/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            locationDeviceService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
